//
//  BZUCLoginVC.m
//  BZUCUCComponent
//
//  Created by quheng on 2017/8/29.
//  Copyright © 2017年 quheng. All rights reserved.
//

#import "BZUCLoginVC.h"
#import "BZUCLoginV.h"
#import <Masonry/Masonry.h>
#import "BZUCLoginWrapperV.h"
#import "BZUCInputVModel.h"
#import <YYKit/YYImage.h>
#import "BZBundleKit.h"
#import "BZUCCurrentUser.h"
#import <objc/runtime.h>
#import <objc/message.h>
#import <BZPreHeaderComponent/BZPreHeaderComponent.h>
#import <BZDataBaseSDK/BZDataBaseManager.h>
#import "BZUCUserModel.h"
#import "BZUCComponentHeader.h"

@interface BZUCLoginVC ()<BZUCLoginVWrapperDelegate>


@property (nonatomic, copy) NSArray *viewData;
@property (nonatomic, weak) BZUCLoginWrapperV *loginWrapperV;
@end

@implementation BZUCLoginVC

- (void)viewDidLoad {

    [super viewDidLoad];
    
    BZUCUserModel *user = [[BZUCUserModel alloc]init];
    
    NSError *error = nil;

    [BZDataBaseManager initialDbByName:@"bzuc"
                            tableNames:@[@"uctable"]
                         tableVersions:@[@(1.0)]
                           tableModels:@[user]
                              outError:&error];

    BZUCLoginWrapperV *loginWrapperV = [[BZUCLoginWrapperV alloc]init];
    loginWrapperV.delegate = self;
    loginWrapperV.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:loginWrapperV];
    
//    self.automaticallyAdjustsScrollViewInsets = NO;
    loginWrapperV.contentSize = CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT);
    [loginWrapperV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).with.offset(0);
        make.right.equalTo(self.view);
        make.bottom.equalTo(self.view);
        make.left.equalTo(self.view);
    }];
    
    self.loginWrapperV = loginWrapperV;

}



- (NSArray *)viewData{

    if (!_viewData) {
        
        BZUCInputVModel *accountVModel = [[BZUCInputVModel alloc]init];
        accountVModel.titleColor = [UIColor blackColor];
        accountVModel.placeholderText = @"请输入手机号码";
        accountVModel.titleFont = [UIFont systemFontOfSize:15];
        accountVModel.placeholderColor = [UIColor colorWithRed:0.64 green:0.51 blue:0.43 alpha:1.00];
        accountVModel.iconVWidth = 20;
        accountVModel.operationBtnWidth = 0;
        accountVModel.textLimit = 11;
        accountVModel.shouldShowTextFDeleted = YES;
        UIImage *image;
        
        image = [BZBundleResourcesKit imageWithName:kAccountImageName
                                               type:kImageType
                                         bundleName:kBundleName
                                        inDirectory:kBundleDirectory];
        
        accountVModel.iconImage = image;
        accountVModel.keyboardType = UIKeyboardTypeNumberPad;
        
        BZUCInputVModel *pswVModel = [[BZUCInputVModel alloc]init];
        pswVModel.keyboardType = UIKeyboardTypeDefault;
        pswVModel.shouldShowTextFDeleted = NO;
        pswVModel.titleColor = [UIColor blackColor];
        pswVModel.placeholderText = @"请输入密码,最少六位";
        pswVModel.placeholderColor = [UIColor colorWithRed:0.64 green:0.51 blue:0.43 alpha:1.00];
        pswVModel.titleFont = [UIFont systemFontOfSize:15];
        pswVModel.iconVWidth = 20;
        pswVModel.operationBtnWidth = 20;
        pswVModel.textLimit = 20;
        
        
        image = [BZBundleResourcesKit imageWithName:kPasswordImageName
                                               type:kImageType
                                         bundleName:kBundleName
                                        inDirectory:kBundleDirectory];        
        pswVModel.iconImage = image;
        
        
        UIImage *normalImage = [BZBundleResourcesKit imageWithName:kPasswordInvisibleImageName
                                                              type:kImageType
                                                        bundleName:kBundleName
                                                       inDirectory:kBundleDirectory];
        
        UIImage *selectedImage = [BZBundleResourcesKit imageWithName:kPasswordVisibleImageName
                                                                type:kImageType
                                                          bundleName:kBundleName
                                                         inDirectory:kBundleDirectory];
        
        pswVModel.buttonImage = normalImage;
        pswVModel.buttonSelectedImage = selectedImage;
        pswVModel.seccuretyText = YES;
        _viewData = @[accountVModel,pswVModel];

    }
    return _viewData;
}


- (void)viewWillAppear:(BOOL)animated{

    [super viewWillAppear:animated];
    
    
    for (BZUCInputVModel *model in self.viewData) {
        
        if ([BZUCCurrentUser shareInstance].userModel) {
            
            if ([self.viewData indexOfObject:model] == 0) {
                model.contentText = [BZUCCurrentUser shareInstance].userModel.phone;
            }else{
                model.contentText = [BZUCCurrentUser shareInstance].userModel.password;
            }
        }
    }
    
    self.loginWrapperV.viewData = self.viewData;
    
    if (!self.navigationController.navigationBar.isHidden) {
        self.navigationController.navigationBar.hidden = YES;
    }
    
}


- (void)viewWillDisappear:(BOOL)animated{

    [super viewWillDisappear:animated];
    
    if (self.navigationController.navigationBar.isHidden) {
        self.navigationController.navigationBar.hidden = NO;
    }
    

}

- (void)loginSuccess:(BZUCLoginWrapperV *)loginWrapper{
    
    Class object =  NSClassFromString(@"MainTabBarViewController");
    id instanceObject = [[object alloc]init];
    if (!instanceObject) {
       instanceObject = [[NSClassFromString(@"BZBlankVC") alloc]init];
        [self.navigationController pushViewController:instanceObject animated:NO];
    }else{
        //SEL methodSel =  NSSelectorFromString(@"configureTabBar");
        //UITabBarController *controller = [instanceObject performSelector:methodSel withObject:nil];
        [UIApplication sharedApplication].keyWindow.rootViewController = instanceObject;
    }
    
    
//    ((void(*)(id,SEL, id,id))objc_msgSend)(instanceObject, methodSel, nil, nil);
    

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
